home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
Other Langs
/
MacPerl ƒ
/
Perl Source ƒ
/
Perl
/
missing.c
< prev
next >
Wrap
Text File
|
1993-12-20
|
4KB
|
263 lines
/* $Header: missing.c $
*
* Copyright (c) 1991, 1992 Matthias Neeracher
*
* You may distribute under the terms of the Perl Artistic License,
* as specified in the README file.
*
*/
#define RESOLVE_MAC_CONFLICTS
#include "EXTERN.h"
#include "perl.h"
#include <Folders.h>
#include <Events.h>
#include <GUSI.h>
/* Allocate more stdio buffers */
FILE _iob[64] = {
0, nil, nil, nil, 0, _IOREAD, 0,
0, nil, nil, nil, 0, _IOWRT, 1,
0, nil, nil, nil, 0, _IOWRT+_IOLBF, 2
};
FILE * _lastbuf = _iob + 64;
/* Calls that don't exist on the mac */
/* Borrowed from msdos.c
* Just pretend that everyone is a superuser
*/
#define ROOT_UID 0
#define ROOT_GID 0
int
getuid(void)
{
return ROOT_UID;
}
int
geteuid(void)
{
return ROOT_UID;
}
int
getgid(void)
{
return ROOT_GID;
}
int
getegid(void)
{
return ROOT_GID;
}
int
setuid(int uid)
{
return (uid==ROOT_UID?0:-1);
}
int
setgid(int gid)
{
return (gid==ROOT_GID?0:-1);
}
execv()
{
fatal("execv() not implemented on the Macintosh");
}
execvp()
{
fatal("execvp() not implemented on the Macintosh");
}
utime()
{
fatal("utime() not implemented on the Macintosh");
}
chmod()
{
}
kill()
{
fatal("kill() not implemented on the Macintosh");
}
sleep(int seconds)
{
long ticks = TickCount() + seconds*60;
while (TickCount() < ticks)
SpinMacCursor();
}
do_aspawn()
{
fatal("do_aspawn() not implemented on the Macintosh");
}
do_spawn()
{
fatal("do_spawn() not implemented on the Macintosh");
}
char **environ;
extern int StandAlone;
char ** init_env(char ** env)
{
int envcnt = 0;
int envsize = 0;
int varlen;
char * envpool;
FILE * envfile = 0;
for (envcnt = 0; env[envcnt]; envcnt++) {
varlen = strlen(env[envcnt]);
envsize += varlen+strlen(env[envcnt]+varlen+1)+2;
}
environ = (char **) malloc((envcnt+1)*sizeof(char *));
envpool = (char *) malloc(envsize);
for (envcnt = 0; env[envcnt]; envcnt++) {
environ[envcnt] = envpool;
varlen = strlen(env[envcnt]);
strcpy(envpool, env[envcnt]);
envpool += varlen+1;
envpool[-1] = '=';
strcpy(envpool, env[envcnt]+varlen+1);
envpool += strlen(env[envcnt]+varlen+1)+1;
}
environ[envcnt] = 0;
return environ;
}
typedef struct PD {
struct PD * next;
FILE * tempFile;
FSSpec pipeFile;
char * execute;
} PipeDescr, *PipeDescrPtr;
static PipeDescrPtr pipes = nil;
static Boolean sweeper = false;
void sweep()
{
while (pipes)
mypclose(pipes->tempFile);
}
FILE * mypopen(char * command, char * mode)
{
PipeDescrPtr pipe;
New(666, pipe, 1, PipeDescr);
if (!pipe)
return NULL;
if (FSpMakeTempFile(&pipe->pipeFile))
goto failed;
pipe->execute = nil;
switch(*mode) {
case 'r':
/* Ugh ! A hardcoded command ! */
if (!strcmp(command, "pwd") || !strcmp(command, "Directory")) {
char curdir[256];
if (!(pipe->tempFile = fopen(FSp2FullPath(&pipe->pipeFile), "w")))
goto delete;
if (!getcwd(curdir, 256))
goto delete;
fprintf(pipe->tempFile, "%s\n", curdir);
fclose(pipe->tempFile);
} else if (SubLaunch(command, nil, &pipe->pipeFile, nil))
goto delete;
if (!(pipe->tempFile = fopen(FSp2FullPath(&pipe->pipeFile), "r")))
goto delete;
break;
case 'w':
New(667, pipe->execute, strlen(command)+1, char);
if (!pipe->execute || !(pipe->tempFile = fopen(FSp2FullPath(&pipe->pipeFile), "w")))
goto delete;
strcpy(pipe->execute, command);
break;
}
pipe->next = pipes;
pipes = pipe;
if (!sweeper) {
atexit(sweep);
sweeper = true;
}
return pipe->tempFile;
delete:
if (pipe->execute)
Safefree(pipe->execute);
HDelete(pipe->pipeFile.vRefNum, pipe->pipeFile.parID, pipe->pipeFile.name);
failed:
Safefree(pipe);
return NULL;
}
int mypclose(FILE * f)
{
OSErr err;
PipeDescrPtr * prev;
PipeDescrPtr pipe;
for (prev = (PipeDescrPtr *) &pipes; pipe = *prev; prev = &pipe->next)
if (pipe->tempFile == f)
break;
if (!pipe)
return -1;
*prev = pipe->next;
fclose(f);
if (pipe->execute)
err = SubLaunch(pipe->execute, &pipe->pipeFile, nil, nil);
else
err = noErr;
HDelete(pipe->pipeFile.vRefNum, pipe->pipeFile.parID, pipe->pipeFile.name);
if (pipe->execute)
Safefree(pipe->execute);
Safefree(pipe);
return err?-1:0;
}
void SpinMacCursor()
{
(GUSIGetSpin())(SP_AUTO_SPIN, 1);
}
void init_missing()
{
environ = nil;
pipes = nil;
}